package com.yang.common.core.domain;


import com.yang.common.core.exception.BusinessException;

/**
 * @author: Billyang
 * @date: 2024/9/17 22:38
 */
public enum ResultCode {
    SUCCESS(0, "成功"),
    FAIL(-1, "失败"),
    FALLBACK(2, "API熔断"),
    /* 自定义结果码 */
    SYSTEM_ERROR(1001, "系统错误"),
    PARAM_INVALID(1002, "参数无效"),
    NO_LOGIN(9000, "未登录"),
    LOGIN_NEED_MOBILE(9999, "登录需要绑定手机号");

    private Integer code;
    private String message;

    ResultCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public static ResultCode of(Integer code) {
        for (ResultCode item : ResultCode.values()) {
            if (item.getCode().equals(code)) {
                return item;
            }
        }
        return null;
    }

    public static ResultCode of(BusinessException businessException) {
        ResultCode resultCode = ResultCode.FAIL;
        resultCode.code = businessException.getCode();
        resultCode.message = businessException.getMessage();
        return resultCode;
    }
}
